<?php


namespace App\Utils;


class Format
{

    public static function time($time)
    {
        $time = time() - $time;
        $day = floor($time / 86400);
        $free = $time % 86400;
        if ($day > 0) {
            return $day . "天前";
        } else {
            if ($free > 0) {
                $hour = floor($free / 3600);
                $free = $free % 3600;
                if ($hour > 0) {
                    return $hour . "小时前";
                } else {
                    if ($free > 0) {
                        $min = floor($free / 60);
                        $free = $free % 60;
                        if ($min > 0) {
                            return $min . "分钟前";
                        } else {
                            if ($free > 0) {
                                return $free . "秒前";
                            }
                        }
                    }
                }
            }
        }
        return '刚刚';
    }

    public static function money($txFee)
    {
        $len = strlen($txFee);
        if ($len <= 18) {
            $txFee = '0.' . str_pad($txFee, 18, '0', STR_PAD_LEFT);
        } else {
            $txFee = substr($txFee, 0, $len - 18) . '.' . substr($txFee, -18);
        }
        return $txFee;
    }

    public static function hashRate($hashRate, $rate = 1)
    {
        if ($hashRate == 0) {
            return "0";
        } else {
            $hashRate = $hashRate * $rate;
            if ($hashRate < 1000) {
                return $hashRate;
            }
            $hashRate = round($hashRate / 1000, 2);
            if ($hashRate < 1000) {
                return $hashRate . 'K';
            }
            $hashRate = round($hashRate / 1000, 2);
            if ($hashRate < 1000) {
                return $hashRate . 'M';
            }
            $hashRate = round($hashRate / 1000, 2);
            if ($hashRate < 1000) {
                return $hashRate . 'G';
            }
            $hashRate = round($hashRate / 1000, 2);
            if ($hashRate < 1000) {
                return $hashRate . 'T';
            }
            $hashRate = round($hashRate / 1000, 2);
            if ($hashRate < 1000) {
                return $hashRate . 'P';
            }

            $hashRate = round($hashRate / 1000, 2);
            return $hashRate . 'E';
        }
    }

    public static function adminProfit($profit)
    {
        $index = strpos($profit, '.');
        if ($index) {
            $len = strlen($profit);
            if ($len - $index > 6) {
                return substr($profit, 0, 7 - $len + $index);
            }
        }
        return $profit;
    }

    public static function hashRateSplit($hashRate, $suffix = '')
    {
        if ($hashRate == 0) {
            return ["0", "" . $suffix];
        } else {
            if ($hashRate < 1000) {
                return $hashRate;
            }
            $hashRate = round($hashRate / 1000, 2);
            if ($hashRate < 1000) {
                return [$hashRate, 'K' . $suffix];
            }
            $hashRate = round($hashRate / 1000, 2);
            if ($hashRate < 1000) {
                return [$hashRate, 'M' . $suffix];
            }
            $hashRate = round($hashRate / 1000, 2);
            if ($hashRate < 1000) {
                return [$hashRate, 'G' . $suffix];
            }
            $hashRate = round($hashRate / 1000, 2);
            if ($hashRate < 1000) {
                return [$hashRate, 'T' . $suffix];
            }
            $hashRate = round($hashRate / 1000, 2);
            if ($hashRate < 1000) {
                return [$hashRate, 'P' . $suffix];
            }

            $hashRate = round($hashRate / 1000, 2);
            return [$hashRate, 'E' . $suffix];
        }
    }
}